package utils;

import android.util.Log;

import com.baidu.mapapi.map.BaiduMap;
import com.example.administrator.test.R;
import com.example.administrator.test.passengernearcar.Constant;
import com.example.administrator.test.passengernearcar.PostRoute;
import com.example.administrator.test.passengernearcar.SendLatLngBean;
import com.example.administrator.test.passengernearcar.TurnNearCarBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.GsonConverterFactory;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2016/12/20.
 */
public class ShowNearCar {
    private static int a;
    private static int b;
    private int[] arrSpecial={R.mipmap.special_01,R.mipmap.special_02,R.mipmap.special_03,R.mipmap.special_04,R.mipmap.special_05,R.mipmap.special_06,
            R.mipmap.special_07,R.mipmap.special_08};
    private int[] arrTruck={R.mipmap.truck_01,R.mipmap.truck_02,R.mipmap.truck_03,R.mipmap.truck_04,R.mipmap.truck_05,R.mipmap.truck_06,
            R.mipmap.truck_07,R.mipmap.truck_08};
    public static void ShowNearCar(final BaiduMap baiduMap, String carType,String sStartLatitude, String sStartLongitude, final int j ){
        SendLatLngBean sendInformationBean=new SendLatLngBean();
        sendInformationBean=initData(sendInformationBean,carType,sStartLatitude,sStartLongitude);
        Gson gson=new Gson();
        String route=gson.toJson(sendInformationBean);
        Log.i("tag",route+"----------------------------");
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PostRoute postRoute=retrofit.create(PostRoute.class);
        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route);
        Call<TurnNearCarBean> call=postRoute.postNearCar(body);
        call.enqueue(new Callback<TurnNearCarBean>() {
            @Override
            public void onResponse(Call<TurnNearCarBean> call, final Response<TurnNearCarBean> response) {
                TurnNearCarBean turnNearCarBean =response.body();
                List<TurnNearCarBean.NearCarBean> list= turnNearCarBean.getNearCar();
                for(int i=0;i<list.size();i++){
                    a= (int) (Math.random()*(8-1+1)+1);
                    b= (int) (Math.random()*(8-1+1)+1);
                    String latitude=list.get(i).getPassengerLocLat();
                    String longitude=list.get(i).getPassengerLocLng();
                    //将获取到的经纬度String类型转为double类型
                    double dlatitude=Double.parseDouble(latitude);
                    double dlongitude=Double.parseDouble(longitude);
                    if(a==1){
                        a=R.mipmap.special_01;
                    }else if(a==2){
                        a=R.mipmap.special_02;
                    }else if(a==3){
                        a=R.mipmap.special_03;
                    }else if(a==4){
                        a=R.mipmap.special_04;
                    }else if(a==5){
                        a=R.mipmap.special_05;
                    }else if(a==6){
                        a=R.mipmap.special_06;
                    }else if(a==7){
                        a=R.mipmap.special_07;
                    }else if(a==8){
                        a=R.mipmap.special_08;
                    }
                    if(b==1){
                        b=R.mipmap.truck_01;
                    }else if(b==2){
                        b=R.mipmap.truck_02;
                    }else if(b==3){
                        b=R.mipmap.truck_03;
                    }else if(b==4){
                        b=R.mipmap.truck_04;
                    }else if(b==5){
                        b=R.mipmap.truck_05;
                    }else if(b==6){
                        b=R.mipmap.truck_06;
                    }else if(b==7){
                        b=R.mipmap.truck_07;
                    }else if(b==8){
                        b=R.mipmap.truck_08;
                    }
                    //调用贴图的方法
                    if(j==0){//j==0贴专车的图，j!=0贴货车的图
                        NearCar.nearCar(dlatitude,dlongitude,baiduMap, a);
                    }else{
                        NearCar.nearCar(dlatitude,dlongitude,baiduMap, b);
                    }
                }
            }
            @Override
            public void onFailure(Call<TurnNearCarBean> call, Throwable t) {
                Log.i("tag","出错");
            }
        });
    }
    private static SendLatLngBean initData(SendLatLngBean sendLatLngBean,String carType, String sStartLatitude, String sStartLongitude){
            sendLatLngBean.setOperType("2004");
            sendLatLngBean.setCarType(carType);
            sendLatLngBean.setPassengerLocLat(sStartLatitude);
            sendLatLngBean.setPassengerLocLng(sStartLongitude);
            return sendLatLngBean;
    }
}
